package com.plan.controller.card;

import com.plan.base.constant.BaseCode;
import com.plan.base.constant.Constant;
import com.plan.base.entity.Result;
import com.plan.controller.card.request.BaseReq;
import com.plan.controller.card.response.CardListVO;
import com.plan.controller.user.response.UserVO;
import com.plan.entity.card.Card;
import com.plan.enums.DelFlagEnum;
import com.plan.service.card.CardService;
import com.plan.service.card.TeamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 积分卡
 *
 * @author zcl
 */
@Slf4j
@RestController
@RequestMapping("pointCard/")
public class CardController {
    @Resource
    private CardService cardService;
    @Resource
    private TeamService teamService;

    /**
     * 创建积分卡
     * 创建时需要初始化以下几个数据：todo
     * 1. 按年龄选择不同的系统默认的任务库，以及不同的积分卡模板
     */
    @PostMapping(value = "add")
    public Result<Long> add(@RequestBody Card card, HttpServletRequest httpRequest) {
        UserVO vo = (UserVO) httpRequest.getAttribute(Constant.USER_KEY);
        Result<Long> check = this.checkParam(card, vo);
        if (!check.isOk()) {
            return check;
        }
        card.setCreatorId(vo.getId());
        card.setUpdaterId(vo.getId());
        card.setTotalPoint(0);
        card.setRemainPoint(0);
        card.setVersion(0);
        card.setDelFlag(DelFlagEnum.NORMAL.getType());

        return cardService.insert(card);
    }

    /**
     * 修改积分卡
     * 修改时，如果年龄变化，则系统默认的任务库、积分卡模板会发生变化：todo
     */
    @PostMapping(value = "update")
    public Result<Long> update(@RequestBody Card card, HttpServletRequest httpRequest) {
        if (card.getId() == null) {
            return Result.fail(BaseCode.PARAM_ERROR);
        }
        UserVO vo = (UserVO) httpRequest.getAttribute(Constant.USER_KEY);
        Result<Long> check = this.checkParam(card, vo);
        if (!check.isOk()) {
            return check;
        }
        card.setUpdaterId(vo.getId());
        return cardService.update(card);
    }

    @PostMapping(value = "delete")
    public Result<Long> delete(@RequestBody BaseReq req, HttpServletRequest httpRequest) {
        if (req.getCardId() == null || req.getTeamId() == null) {
            return Result.fail(BaseCode.PARAM_ERROR);
        }
        UserVO vo = (UserVO) httpRequest.getAttribute(Constant.USER_KEY);
        if (!teamService.isUserInTeam(req.getTeamId(), vo.getId())) {
            return Result.fail(BaseCode.ILLEGAL_OPERATION);
        }
        log.warn("删除积分卡，cardId={}，userId={},username={}", req.getCardId(), vo.getId(), vo.getName());
        return cardService.delete(req, vo);
    }

    @PostMapping(value = "list")
    public Result<List<CardListVO>> list(HttpServletRequest httpRequest) {
        UserVO vo = (UserVO) httpRequest.getAttribute(Constant.USER_KEY);
        return cardService.list(vo.getFamilyId());
    }


    private Result<Long> checkParam(Card card, UserVO vo) {
        if (card.getName() == null || card.getGender() == null || card.getBirthday() == null || card.getTeamId() == null) {
            return Result.fail(BaseCode.PARAM_ERROR);
        }
        if (card.getName().length() > Constant.NAME_MAX_LENGTH || (card.getGender() != 0 && card.getGender() != 1)) {
            return Result.fail(BaseCode.PARAM_ERROR);
        }
        if (!teamService.isUserInTeam(card.getTeamId(), vo.getId())) {
            return Result.fail(BaseCode.ILLEGAL_OPERATION);
        }
        return Result.ok();
    }

}
